<template>
  <div class="users">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item> </el-breadcrumb
    ><br />
    <!-- 搜索框 -->
    <div class="users_div">
      <el-tag>用户列表</el-tag><br />
      <div class="users_div_input">
        <el-input
          @keydown.native.enter="search()"
          placeholder="请输入内容"
          v-model.trim="query"
          class="input-with-select"
        >
          <el-button @click="search()" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
    </div>
    <br />
    <div>
      <el-card class="box-card">
        <el-button type="primary" @click="adduser()">添加</el-button>
        <el-button type="primary">编辑</el-button>
        <el-button type="danger">删除</el-button>
        <el-button style="margin-left: 58%" type="warning">账号授权</el-button>
      </el-card>
    </div>

    <!--主体-->
    <el-table :data="list.users" border style="width: 100%">
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column prop="username" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="id" label="学号" width="120"> </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="120"> </el-table-column>
      <el-table-column prop="email" label="邮箱" width="160"> </el-table-column>
      <el-table-column prop="role_name" label="角色" width="100"> </el-table-column>
      <el-table-column prop="zip" label="专业" width="150"> </el-table-column>
      <el-table-column label="操作" width="145">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="primary" size="small"
            >编辑</el-button
          >
          <el-button
            @click.native.prevent="deleteRow(scope.$index, list.users)"
            type="danger"
            size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="list.total"
      >
      </el-pagination>
    </div>
    <!-- 弹出框 -->
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%">
      <!-- 表单 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        </el-form>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    //邮箱验证
    var validateemail = (rule, value, callback) => {
        if (!/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(value)) {
          callback(new Error('邮箱格式不正确'));
        } else {
          callback();
        }
      };
    //手机号验证
    var validatephone = (rule, value, callback) => {
        if (!/^1[3|4|5|7|8][0-9]{9}$/.test(value)) {
          callback(new Error('手机号格式不正确'));
        } else {
          callback();
        }
      };
    return {
      query: "",
      pagenum: 1,
      pagesize: 5,
      list: {},
      dialogVisible: false,
      ruleForm: {
        username: "",
        password:"",
        phone:"",
        email:""
      },
      rules: {
        username: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ validator: validateemail, trigger: 'blur' }],
        phone: [{ validator: validatephone, trigger: 'blur' }],
        

      },
    };
  },
  methods: {
    //添加表单验证
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    //添加
    adduser() {
      this.dialogVisible = true
    },
    //编辑
    handleClick(row) {
      console.log(row);
    },
    //删除
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    //搜索
    search() {
      this.getList();
    },
    //页码
    //每页数据发生改变
    handleSizeChange(val) {
      this.pagesize = val;
      this.getList();
      console.log(val);
    },
    //改变页码触发
    handleCurrentChange(val) {
      this.pagenum = val;
      this.getList();
    },
    //获取用户列表
    async getList() {
      var p = await this.axios.get(
        `users?query=${this.query}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
      );
      console.log(p);
      this.list = p.data.data;
    },
  },
  mounted() {
    this.getList();
  },
};
</script>
<style lang="scss">
.users_div {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
